package controller

import (
	bc "Agricultural/application/blockchain"
	"bytes"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateQrCode(ctx *gin.Context)  {
	subId := ctx.Query("subId")
	companyId := ctx.Query("companyId")
	fmt.Println(subId, " ", companyId)
	resp, err := bc.ChannelExecute("generateQrCode", [][]byte{
		[]byte(subId),
		[]byte(companyId),
	})
	if err != nil {
		fmt.Println("ERROR: " , err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

func ShowQrCode(ctx *gin.Context)  {
	subId := ctx.Query("subId")
	resp, err := bc.ChannelExecute("showQrCode", [][]byte{
		[]byte(subId),
	})
	if err != nil {
		fmt.Println("ERROR: " , err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.File(bytes.NewBuffer(resp.Payload).String())
	//ctx.String(http.StatusOK, bytes.NewBuffer(resp.Payload).String())
	//ctx.JSON(http.StatusOK, resp)
}